﻿/**************************************************************
 *
 * 命名空间：Sgr.UPMS.Application.Queries.Impl
 * 创建时间：2025/3/20
 * 描述：菜单查询服务实现
 *
 **************************************************************/

using Microsoft.EntityFrameworkCore;
using Sgr.UPMS.Application;
using Sgr.UPMS.Application.Queries;
using Sgr.UPMS.Application.ViewModels;
using Sgr.UPMS.Domain.Menus;

namespace Sgr.UPMS.Infrastructure.Queries
{
    /// <summary>
    /// 菜单查询服务实现
    /// </summary>
    public class MenuQueries : IMenuQueries
    {
        private readonly IUpmsQueryContext _context;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">只读数据库上下文</param>
        public MenuQueries(IUpmsQueryContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 根据ID获取菜单信息
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>菜单视图模型</returns>
        public async Task<OutMenuViewModel?> GetByIdAsync(long id,
            CancellationToken cancellationToken = default)
        {
            var query = _context.Query<Menu>()
                .Where(x => x.Id == id)
                .Select(x => new OutMenuViewModel
                {
                    Id = x.Id,
                    ParentId = x.ParentId,
                    Name = x.Name,
                    Icon = x.Icon,
                    RouteName = x.RouteName,
                    RoutePath = x.RoutePath,
                    ComponentPath = x.ComponentPath,
                    RedirectPath = x.RedirectPath,
                    Permission = x.Permission,
                    Type = x.Type,
                    Description = x.Description,
                    Order = x.Order,
                    ExternalLink = x.ExternalLink,
                    Hidden = x.Hidden,
                    KeepAlive = x.KeepAlive,
                    Affix = x.Affix,
                    IsActive = x.IsActive,
                    Target = x.Target
                });

            return await _context.FirstOrDefaultAsync(query, cancellationToken);
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>菜单列表</returns>
        public async Task<IEnumerable<OutMenuViewModel>> GetListAsync(InMenuSearchModel request,
            CancellationToken cancellationToken = default)
        {
            var query = _context.Query<Menu>().AsQueryable();

            // 应用查询条件
            // 按名称查询
            if (!string.IsNullOrWhiteSpace(request.Name))
            {
                query = query.Where(f => EF.Functions.Like(f.Name, $"{request.Name}%"));
            }

            // 转换为视图模型
            var resultQuery = query.OrderBy(x => x.ParentId)
                .ThenBy(x => x.Order)
                .Select(x => new OutMenuViewModel
                {
                    Id = x.Id,
                    ParentId = x.ParentId,
                    Name = x.Name,
                    Icon = x.Icon,
                    RouteName = x.RouteName,
                    RoutePath = x.RoutePath,
                    ComponentPath = x.ComponentPath,
                    RedirectPath = x.RedirectPath,
                    Permission = x.Permission,
                    Type = x.Type,
                    Description = x.Description,
                    Order = x.Order,
                    ExternalLink = x.ExternalLink,
                    Hidden = x.Hidden,
                    KeepAlive = x.KeepAlive,
                    Affix = x.Affix,
                    IsActive = x.IsActive,
                    Target = x.Target
                });

            return await _context.ToListAsync(resultQuery, cancellationToken);
        }
    }
}